package com.cn.jinl.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cn.jinl.api.ILineBanMqttService;
import com.cn.jinl.dao.LineBanMqttMapper;
import com.cn.jinl.domain.LineBanMqtt;
import com.cn.jinl.utils.CommonUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@Service
public class LineBanMqttServiceImpl implements ILineBanMqttService {

    @Autowired
    private LineBanMqttMapper lineBanMqttMapper;

    private static Map<String,String>namesMap = Maps.newHashMap();
    static {
        namesMap.put("01","B01");
        namesMap.put("02","B02");
        namesMap.put("03","B03");
        namesMap.put("04","B04");
        namesMap.put("05","B05");
    }
    @Override
    public String addBatchLineBanMqtt(JSONArray jsonArray) throws Exception {
        if(jsonArray.size() == 0){
            return "";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<LineBanMqtt> lineBanMqtts = Lists.newArrayList();

        for(int i =0; i < jsonArray.size();i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String mixerstarttime = jsonObject.getString("mixerstarttime");
            if(StringUtils.equals("0001-01-01T00:00:00",mixerstarttime)){
                continue;
            }
            String startTime = outputFormat.format(simpleDateFormat.parse(mixerstarttime));
            String name = jsonObject.getString("name");
            if(!namesMap.containsKey(name)){
                continue;
            }
            LineBanMqtt banMqtt = new LineBanMqtt();
            banMqtt.setStartTime(startTime);
            banMqtt.setBanName(name);
            String deviceCode = namesMap.get(name);
            banMqtt.setBanAlias(deviceCode);
            String mixerstoptime = jsonObject.getString("mixerstoptime");
            if(!StringUtils.equals("0001-01-01T00:00:00",mixerstoptime)){
                String endTime = outputFormat.format(simpleDateFormat.parse(mixerstoptime));
                banMqtt.setEndTime(endTime);
            }
            String TimeStamp = jsonObject.getString("TimeStamp");
            String timestamp = outputFormat.format(simpleDateFormat.parse(TimeStamp));
            banMqtt.setTimeStamp(timestamp);
            String mixerDo = jsonObject.getString("mixerDo");
            banMqtt.setMixDo(mixerDo);
            boolean mixerflag = jsonObject.getBoolean("mixerflag");
            banMqtt.setMixFlag(mixerflag?"0":"1");
            int mixertime = jsonObject.getInteger("mixertime");
            banMqtt.setMixTime(mixertime+"");
            lineBanMqtts.add(banMqtt);
        }
        if(CollectionUtils.isEmpty(lineBanMqtts)){
            return "";
        }

        int count = lineBanMqttMapper.batchInsertLineBanMqtt(lineBanMqtts);
        return CommonUtil.outStr(count);
    }

    public static void main(String args[])throws Exception{
        String t = "2024-03-22T14:34:28.5902464+08:00";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time1 = outputFormat.format(simpleDateFormat.parse(t));
        System.out.println(time1);
    }
}
